﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.FMCG.Buz.Models;
using Microhard.FMCG.Buz.Interfaces;

namespace Microhard.FMCG.Buz.Impls.KASystem.WUMart
{
    public class OrderDetailParser : IHtmlParser<List<OrderDetailModel>>
    {
        public List<OrderDetailModel> ParseObject(string html)
        {
            List<OrderDetailModel> result = new List<OrderDetailModel>();
            if (string.IsNullOrEmpty(html))
                return result;
            string[] rows = html.Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            //获得信息位置
            int skuindex = 0;
            int itemnameIndex = 0;
            int orderQtyIndex = 0;
            int zstoreNOIndex = 0;
            int ponoIndex = 0;
            var top = rows[0].Split(new char[] { ',' });
            for (int t = 0; t < top.Length; t++)
            {

                if (top[t] == "PO_NO")
                {
                    ponoIndex = t;
                }
                if (top[t] == "SKU")
                {
                    skuindex = t;
                }
                if (top[t] == "Item_Name")
                {
                    itemnameIndex = t;
                }
                if (top[t] == "Order_Qty")
                {
                    orderQtyIndex = t;
                }
                if (top[t] == "ZStore_NO")
                {
                    zstoreNOIndex = t;
                }
            }
            //获得订单信息
            for (int n = 1; n < rows.Length; n++)
            {
                string[] cells = rows[n].Split(new string[] { "," }, StringSplitOptions.None);
                OrderDetailModel detail = new OrderDetailModel();
                detail.SKU = cells[skuindex];
                detail.PrdName = cells[itemnameIndex];
                detail.OrderQty = double.Parse(cells[orderQtyIndex]);
                detail.StoreNo = cells[zstoreNOIndex];
                detail.OrderCode = cells[ponoIndex];
                result.Add(detail);
            }
            return result;
        }
    }
}
